import {
    ajaxPost,
    showToast
} from '../../../utils/util'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        backShow: false, // 返回按钮
        inviteVal: 0, //邀请状态 0 =>未邀请1 已邀请
        collectVal: false, //收藏控制
        companySource: {}, //企业信息
        workSource: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.type == 'share') {
            this.setData({
                backShow: true
            })
        }
        let data = {
            token: wx.getStorageSync('token'),
            company_uid: options.companyId,
        }
        ajaxPost('/authuser/xxcompanyinfo', data, res => {
            if (res.code == 0) {
                this.setData({
                    collectVal: res.data.is_follow == 0 ? false : true,
                    companySource: res.data.company_info,
                    meetingCountSource: res.data.meeting_count,
                    workSource: res.data.position_list,
                    inviteVal: res.data.is_invitation
                })
            }
        })
    },

    /**
     * 分享
     */
    onShareAppMessage() {
        return {
            title: `公司详情`,
            path: `/pages/share/share?identity=student&type=company&shareId=${this.data.companySource.uid}`,
            success: function (res) {
                showToast('text', '分享成功')
            }
        }
    },

    /**
     * 收藏操作
     */
    collectClick() {
        let data = {
            token: wx.getStorageSync('token'),
            company_uid: this.data.companySource.uid
        }
        ajaxPost('/authuser/followcompany', data, res => {
            if (res.code == 0) {
                if (this.data.collectVal) {
                    showToast('text', '取消关注成功')
                } else {
                    showToast('text', '关注成功')
                }
                this.setData({
                    collectVal: !this.data.collectVal
                })

            } else {
                showToast('text', res.msg)
            }
        })
    },
    /**
     * 邀请点击事件
     */
    inviteClick() {
        if (this.data.inviteVal == 1) return;
        let data = {
            token: wx.getStorageSync('token'),
            company_uid: this.data.companySource.uid,
        }
        ajaxPost('/authuser/invitationCompany', data, res => {
            if (res.code == 0) {
                showToast('text', '邀请成功')
                this.setData({
                    inviteVal: 1,
                })
            }
        })
    }
})